home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 1.iso
/
toolbox
/
src
/
exampleCode
/
opengl
/
defrasterPort
/
defx.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-11-11
|
6KB
|
288 lines
/*
* Copyright (c) 1994, Silicon Graphics, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that the name of Silicon Graphics may not be used in any advertising or
* publicity relating to the software without the specific, prior written
* permission of Silicon Graphics.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
* INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE
* POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* OpenGL(TM) is a trademark of Silicon Graphics, Inc.
*/
/*
* Example to convert (and render) a font defined by defrasterfont()
* in irisGL to a X bitmap
*
* Yusuf Attarwala
*
* cc defx.c -o defx -lX11
*
*/
#include <X11/Xlib.h>
#include <X11/Xutil.h>
/* test_raster is compatible to defraster font */
int test_width = 33;
int test_height = 33;
unsigned short test_raster[] = {
0x000f, 0xf000, 0x0000,
0x0010, 0x0800, 0x0000,
0x0060, 0x0600, 0x0000,
0x0180, 0x0180, 0x0000,
0x0600, 0x0060, 0x0000,
0x0800, 0x0010, 0x0000,
0x0803, 0xc010, 0x0000,
0x100f, 0xf008, 0x0000,
0x1038, 0x1c08, 0x0000,
0x2060, 0x0604, 0x0000,
0x2080, 0x0104, 0x0000,
0x4000, 0x0002, 0x0000,
0x8000, 0x0001, 0x0000,
0x8003, 0xc001, 0x0000,
0x8003, 0xc001, 0x0000,
0x8001, 0x8001, 0x0000,
0x8000, 0x0001, 0x0000,
0x8000, 0x0001, 0x0000,
0x8070, 0x0e01, 0x0000,
0x8070, 0x0e01, 0x0000,
0x8070, 0x0e01, 0x0000,
0x4070, 0x0e02, 0x0000,
0x2000, 0x0004, 0x0000,
0x2000, 0x0004, 0x0000,
0x1000, 0x0008, 0x0000,
0x1000, 0x0008, 0x0000,
0x0800, 0x0010, 0x0000,
0x0800, 0x0010, 0x0000,
0x0600, 0x0060, 0x0000,
0x01ff, 0xff80, 0x0000,
0x007f, 0xfe00, 0x0000,
0x001f, 0xf800, 0x0000,
0x000f, 0xf000, 0x0000};
unsigned char *defToX();
unsigned char flipBit(),mirror();
#define BITUNIT 8
int width,height;
Display *display;
int screen;
Window win;
XVisualInfo *vi;
GC gc;
Pixmap stip;
/* for testing
static char tbm[] = {0x7c,
0xc3,
0xc0,
0xc0,
0xfe,
0xc3,
0xc3,
0xc3,
0x7e,
0x00};
*/
void
testBitmap(xbm)
unsigned char *xbm;
{
XEvent report;
display = XOpenDisplay (NULL);
screen = DefaultScreen(display);
win = XCreateSimpleWindow (display, RootWindow(display,screen),
0, 0, 200, 200,
2, BlackPixel(display,screen),
WhitePixel(display,screen));
XMapRaised(display,win);
XSelectInput (display, win, ExposureMask);
gc = XCreateGC(display,win,0,0);
XSetForeground (display, gc,BlackPixel(display,screen));
XSetBackground (display, gc,WhitePixel(display,screen));
stip = XCreateBitmapFromData(display,RootWindow(display,screen),
xbm,width,height);
/* event loop */
for (;;)
{
XNextEvent (display, &report);
switch (report.type)
{
case Expose:
drawIt();
break;
}
}
}
drawIt()
{
unsigned long planemask = 1;
XClearWindow(display,win);
XCopyPlane(display,stip,win,gc,0,0,width,height,20,20,planemask);
XFlush(display);
}
void
main()
{
unsigned char *xbm;
xbm = defToX(test_width,test_height,test_raster);
testBitmap(xbm);
}
unsigned char *
defToX(w,h,r)
int w,h;
unsigned short *r;
{
unsigned char *xbm,*uc;
unsigned short *us;
int i,j;
int wide;
int tokensPerRow;
int outTokensPerRow;
width = w;
height = h;
if (width <= 16) {
tokensPerRow = 1;
}
else {
if (width%16 == 0) {
tokensPerRow = (int)(width / 16);
}
else {
tokensPerRow = (int)(width / 16) + 1;
}
}
if (width <= BITUNIT) {
outTokensPerRow = 1;
}
else {
if (width%BITUNIT == 0) {
outTokensPerRow = (int)(width / BITUNIT);
}
else {
outTokensPerRow = (int)(width / BITUNIT) + 1;
}
}
xbm = (unsigned char *)malloc(BITUNIT * outTokensPerRow * height);
us = r;
for (i=height-1;i>=0;i--) { /* top bottom */
uc = xbm + i*outTokensPerRow;
wide = 0;
for (j=0;j<tokensPerRow;j++) {
*uc = (unsigned char)(*us >> 8);
*uc = flipBit(*uc);
uc++;
wide += 8;
if (wide < width) {
*uc = (unsigned char)(*us);
*uc = flipBit(*uc);
uc++;
wide += 8;
}
us++;
}
}
return(xbm);
}
unsigned char
flipBit(uc)
{
unsigned char left,right,ouc;
static unsigned char rightmask = 0x0f;
static unsigned char leftmask = 0xf0;
left = (uc & leftmask) >> 4;
right = (uc & rightmask) << 4;
ouc = mirror(right) | mirror(left);
return(ouc);
}
unsigned char
mirror(uc)
{
unsigned char ret;
static unsigned char mirlut[241];
static int firstTime = 1;
if (firstTime) {
firstTime = 0;
mirlut[0] = 0;
mirlut[1] = 8;
mirlut[2] = 4;
mirlut[3] = 12;
mirlut[4] = 2;
mirlut[5] = 10;
mirlut[6] = 6;
mirlut[7] = 14;
mirlut[8] = 1;
mirlut[9] = 9;
mirlut[10] = 5;
mirlut[11] = 13;
mirlut[12] = 3;
mirlut[13] = 11;
mirlut[14] = 7;
mirlut[15] = 15;
mirlut[16] = 128;
mirlut[32] = 64;
mirlut[48] = 192;
mirlut[64] = 32;
mirlut[80] = 160;
mirlut[96] = 96;
mirlut[112] = 224;
mirlut[128] = 16;
mirlut[144] = 144;
mirlut[160] = 80;
mirlut[176] = 208;
mirlut[192] = 48;
mirlut[208] = 176;
mirlut[224] = 112;
mirlut[240] = 240;
}
return(mirlut[uc]);
}